// pages/register/register.js
const util = require('../../utils/util.js')
var app = getApp()  
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: null,
    userName: null,
    gender: null,
    studentId: null,
    password:null,
    department: null,
    major: null,
    wechatId: null,
    phoneNum: null,
    favorableNum: 0,
    coolects: null,
    avatar: null,
    nickID:null,
    //用户个人信息  
    userInfo: {}    
  },

  add_btn: function(){
    if(this.data.userName==null){
      wx.showModal({
        title: '提示',
        content: '姓名不能为空',
      })
    } else if (this.data.gender==null){
      wx.showModal({
        title: '提示',
        content: '请选择性别',
      })
    }else if(this.data.studentId==null){
      wx.showModal({
        title: '提示',
        content: '学号不能为空',
      })
    } else if(this.data.password==null){
      wx.showModal({
        title: '提示',
        content: '密码不能为空',
      })
    } else if (this.data.department==null){
      wx.showModal({
        title: '提示',
        content: '院系不能为空',
      })
    }else if(this.data.major==null){
      wx.showModal({
        title: '提示',
        content: '专业不能为空',
      })
    }else if(this.data.phoneNum==null){
      wx.showModal({
        title: '提示',
        content: '手机号不能为空',
      })
    }else{
      if (this.data.studentId.length != 13) {
        wx.showModal({
          title: '提示',
          content: '学号格式错误',
        })
      } else if(this.data.phoneNum.length != 11) {
        wx.showModal({
          title: '提示',
          content: '电话格式错误',
        })
    }else{
        util.addUser(this.data.userName, this.data.gender, this.data.studentId, this.data.password, this.data.department, this.data.major, this.data.phoneNum, this.data.avatar, function () {
          wx.switchTab({
            // url: '/pages/login/login'
            url: '/pages/discover/discover'

          })
        })
    }   

    }  
    
  },

  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  
  radioChange: function(e){
     if (e.detail.value=='F'){
      this.setData({
        gender: '女',
        avatar:'http://120.77.247.244:8080/user/timg.jpg'
      })
    }
     if (e.detail.value == 'M'){
      this.setData({
        gender: '男',
        avatar:'http://120.77.247.244:8080/user/timg1.jpg'
      })
      }
  },

  studentIdInput: function (e) {
    this.setData({
      studentId: e.detail.value
    })
  },

  passwordInput: function(e){
    this.setData({
      password: e.detail.value
    })
  },

  departmentInput: function (e) {
    this.setData({
      department: e.detail.value
    })
  },

  majorInput: function (e) {
    this.setData({
      major: e.detail.value
    })
  },

  phoneNumInput: function (e) {
    this.setData({
      phoneNum: e.detail.value
    })
  },

  chooseImage: function () {
    var that = this
    wx.chooseImage({
      success: function (res) {
        that.setData({
          imageList: res.tempFilePaths
        })
      }
    })
  },
  previewImage: function (e) {
    var current = e.target.dataset.src

    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // /**  
    //  * 获取用户信息  
    //  */
    // var that = this
    // //调用应用实例的方法获取全局数据  
    // app.getUserInfo(function (userInfo) {
    //   //更新数据  
    //   that.setData({
    //   userInfo: userInfo
    //   })
    // }) 

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
var sex = [{
  key: "1",
  value: "男"
}, {
  key: "2",
  value: "女"
}];